package cn.pzx.base_module.base

import android.app.ProgressDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import cn.pzx.base_module.event.EventMessage
import cn.pzx.base_module.http.ErrorResult
import com.blankj.utilcode.util.ToastUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.lang.reflect.ParameterizedType

/**
 * @author pzx
 * @description:
 * @date :3/10/21 9:42 AM
 */
abstract class BaseFragment<VM : BaseViewModel, VB : ViewBinding> : Fragment() {


    lateinit var mContext: FragmentActivity
    var contentView: View? = null
    lateinit var vm: VM
    lateinit var vb: VB
    private var loadingDialog: ProgressDialog? = null

    //Fragment的View加载完毕的标记
    private var isViewCreated = false

    //Fragment对用户可见的标记
    private var isUIVisible = false

    var isVisibleToUser = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val type = javaClass.genericSuperclass as ParameterizedType
        val clazz1 = type.actualTypeArguments[0] as Class<VM>
        vm = ViewModelProvider(this).get(clazz1)

        val clazz2 = type.actualTypeArguments[1] as Class<VB>
        val method = clazz2.getMethod("inflate", LayoutInflater::class.java)
        vb = method.invoke(null, layoutInflater) as VB

        mContext = context as AppCompatActivity


    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (null == contentView) {
            contentView = vb.root
            init()
            initView()
            initClick()
            initData()
            initVM()
        }

        return contentView
    }

    private fun init() {

        if (getIsRegisterBus()) EventBus.getDefault().register(this)
        //loading
        vm.isShowLoading.observe(viewLifecycleOwner, Observer {
            if (it) showLoading() else dismissLoding()
        })
        //错误信息
        vm.errorData.observe(viewLifecycleOwner, Observer {
            if (it.show) ToastUtils.showShort(it.errMsg)
            errorResult(it)
        })


    }

    fun getIsRegisterBus(): Boolean {
        return false
    }

    abstract fun initVM()

    abstract fun initView()

    abstract fun initClick()

    abstract fun initData()


    private fun showLoading() {
        if (loadingDialog == null) {
            loadingDialog = ProgressDialog(mContext)
        }
        loadingDialog!!.show()
    }

    private fun dismissLoding() {
        loadingDialog?.dismiss()
        loadingDialog = null
    }

    /**
     * 接口请求错误回调
     */
    open fun errorResult(errorResult: ErrorResult) {}


    //事件传递
    @Subscribe
    fun onEventMainThread(msg: EventMessage) {
        receivedEvent(msg)
    }

    /**
     * 接收事件
     */
    private fun receivedEvent(eventMessage: EventMessage) {

    }


    override fun onDestroyView() {
        super.onDestroyView()
        if (getIsRegisterBus()) EventBus.getDefault().unregister(this)
        contentView = null
    }


}